/**
 * Filename: AbstractSpinerAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/17 13:50
 */
package com.github.postsanf.yinian.adapter;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;

public class AbstractSpinerAdapter extends BaseAdapter {
	LayoutInflater inflater;
	Context context;
	ArrayList<String> list;
	private Resources	mResources;

	public AbstractSpinerAdapter(Context context, ArrayList<String> list) {
		super();
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
	}

	public void setResources(Resources mResource){
		this.mResources = mResource;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.yn_item_spinner, null);
			viewHolder = new ViewHolder();
			viewHolder.layout = (RelativeLayout) convertView.findViewById(R.id.rl_spinner_item_layout);
			viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_spinner_item);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (mResources!=null){
			viewHolder.layout.setBackgroundColor(mResources.getColor(mResources.getIdentifier("popbg", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
			viewHolder.textView.setTextColor(mResources.getColor(mResources.getIdentifier("border_color", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
		}
		viewHolder.textView.setText(list.get(position));
		return convertView;
	}

	public class ViewHolder {
		RelativeLayout layout;
		TextView textView;
	}

	public void refresh(List<String> l) {
		this.list.clear();
		list.addAll(l);
		notifyDataSetChanged();
	}

	public void add(String str) {
		list.add(str);
		notifyDataSetChanged();
	}

	public void add(ArrayList<String> str) {
		list.addAll(str);
		notifyDataSetChanged();
	}
}